package login;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class NewAccount
 */
@WebServlet("/NewAccount")
public class NewAccount extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewAccount() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.getParameter(""); 
		String username = request.getParameter("username"); 
		String password = request.getParameter("password"); 
		String email = request.getParameter("email"); 
		String firstname = request.getParameter("firstname"); 
		String lastname = request.getParameter("lastname"); 
		String contactnum = request.getParameter("contactNum"); 
		String accountType = "Customer"; 
		String action = request.getParameter("action"); 
		
		
		LoginMachine lm;
		try {
			lm = new LoginMachine();
			if (action.equals("Update")) {
				lm.updateAccount(username, email, password, firstname, lastname, contactnum, accountType); 
				response.sendRedirect("account.jsp"); 
			}
			else {
				int acctCreated = lm.processNewAccount(username, email, password, firstname, lastname, contactnum, accountType);
				if (acctCreated == 1) {
					response.sendRedirect("login.jsp"); 
				}
				else {
					response.sendRedirect("account.jsp?status=failed"); 
				}
			}	
		} catch (ClassNotFoundException e) {
			response.sendRedirect("account.jsp?status=failed");
		} catch (SQLException e) {
			response.sendRedirect("account.jsp?status=failed");
		}
		
		
	}

}
